package com.tj.bo.integration.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Response;
import com.fqgj.exception.common.ApplicationException;
import com.google.common.collect.Maps;
import com.tj.bo.common.conf.util.HttpUtil;
import com.tj.bo.integration.PaymentIntegrationService;
import com.tj.bo.integration.input.PaymentInput;
import com.tj.bo.integration.input.PayoutInput;
import com.tj.bo.integration.output.PaymentChannelOutput;
import com.tj.bo.integration.output.PaymentOutput;
import com.tj.bo.integration.output.PayoutOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: anonymous
 * Date: 2020/06/11
 * Time: 下午2:16
 */
@Service
public class PaymentIntegrationServiceImpl implements PaymentIntegrationService {
    private static final Logger logger = LoggerFactory.getLogger(PaymentIntegrationServiceImpl.class);

    /**
     * 获取支付通道
     */
    @Override
    public List<PaymentChannelOutput> getPaymentChannelList(String payBaseUrl, String productCategory) {
        String url = payBaseUrl + "payment/channel/list";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("productCategory", productCategory);
        Map<String, String> headers = Maps.newHashMap();
        headers.put("Accept", "application/json");
        headers.put("Content-Encoding", "UTF-8");
        headers.put("Content-Type", "application/json; charset=UTF-8");
        String data = HttpUtil.postPageBody(url, headers, jsonObject.toJSONString());
        logger.info("PaymentIntegrationService getPaymentChannelList productCategory:{},data:{}", productCategory, data);
        Response response = JSONObject.parseObject(data, Response.class);
        if (!response.isSuccess()) {
            throw new ApplicationException("get payment channel error");
        }
        JSONArray jsonArray = (JSONArray) response.getData();
        List<PaymentChannelOutput> paymentChannelOutputList = jsonArray.toJavaList(PaymentChannelOutput.class);
        return paymentChannelOutputList;
    }

    @Override
    public PaymentOutput payment(String payBaseUrl, PaymentInput paymentInput) {
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Content-Type", "application/json");
        String data = HttpUtil.postPageBody(payBaseUrl + "payment", headers, JSON.toJSONString(paymentInput));
        logger.info("PaymentIntegrationService payment paymentInput:{},data:{}", JSON.toJSONString(paymentInput), data);
        Response response = JSONObject.parseObject(data, Response.class);
        if (!response.isSuccess()) {
            throw new ApplicationException("payment error");
        }
        JSONObject jsonObject = (JSONObject) response.getData();
        PaymentOutput paymentOutput = jsonObject.toJavaObject(PaymentOutput.class);
        return paymentOutput;
    }

    @Override
    public PayoutOutput payout(String payBaseUrl, PayoutInput payoutInput) {
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Content-Type", "application/json");
        String data = HttpUtil.postPageBody(payBaseUrl + "payout", headers, JSON.toJSONString(payoutInput));
        logger.info("PaymentIntegrationService payout payoutInput:{},data:{}", JSON.toJSONString(payoutInput), data);
        Response response = JSONObject.parseObject(data, Response.class);
        if (!response.isSuccess()) {
            throw new ApplicationException("payment error");
        }
        JSONObject jsonObject = (JSONObject) response.getData();
        return (PayoutOutput) jsonObject.toJavaObject(PayoutOutput.class);
    }
}